package com.ruoyi.repn.task;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.repn.domain.dto.RepOrderDto;
import com.ruoyi.repn.mapper.RepOrderMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.Calendar;
import java.util.Date;
import java.util.List;

import static com.ruoyi.common.constant.ReportConstants.*;

/**
 * @author cpf
 * @date 2024/3/6 13:54
 * @description 定时任务
 */

@Component
public class Task {

    @Autowired
    private RepOrderMapper repOrderMapper;

    /**
     * 定时检查订单支付状态
     */
    //@Scheduled(cron="0/5 * *  * * ? ") //每5秒执行一次
    // 使用cron表达式设置每30分钟执行一次
    @Scheduled(cron = "0 0/30 * * * ?")
    public void execute(){
        List<RepOrderDto> repOrderDtos = repOrderMapper.selectRepOrderList(null);
        if (!repOrderDtos.isEmpty()){
            for (RepOrderDto repOrderDto : repOrderDtos) {
                //查询未支付的订蛋糕
                if (repOrderDto.getPayStatus().equals(NON_PAYMENT)){
                    //当前时间
                    Date currentTime = new Date();
                    //订单创建时间
                    Date createTime = repOrderDto.getCreateTime();
                    //增加15分钟
                    Calendar calendar = Calendar.getInstance();
                    calendar.setTime(createTime);
                    calendar.add(Calendar.MINUTE,15);
                    Date newTime = calendar.getTime();
                    //当前时间大于订单过期时间，修改订单状态
                    if (!currentTime.before(newTime)){
                        repOrderDto.setStatus(CANCELED);
                        repOrderMapper.updateRepOrder(repOrderDto);
                    }
                }
            }
        }

    }

}

